/** Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.spring4facelets.security;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;

/**
 * Used for to make testing security easier by providing query string
 * configuration of authenication and authorization.
 *
 * @author Allen Parslow
 */
public class SecurityForTestingFilter implements Filter {

	/**
	 * {@inheritDoc}
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	/**
	 * {@inheritDoc}
	 */
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
			throws IOException, ServletException {
		SecurityContextHolder.clearContext();

		if (servletRequest instanceof HttpServletRequest && servletResponse instanceof HttpServletResponse) {
			HttpServletRequest request = (HttpServletRequest) servletRequest;
			HttpServletResponse response = (HttpServletResponse) servletResponse;

			String secure = request.getParameter("secure");
			if (!StringUtils.isEmpty(secure)) {
				doSecurity(request, response);
			}
		}

		chain.doFilter(servletRequest, servletResponse);

		SecurityContextHolder.clearContext();
	}

	protected void doSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException,
			ServletException {
		String user = request.getParameter("user");
		if (StringUtils.isEmpty(user)) {
			user = "a-user";
		}
		String[] roles = request.getParameterValues("role");

		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(0);
		if (roles != null) {
			for (String role : roles) {
				authorities.add(new GrantedAuthorityImpl(role));
			}
		}

		UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, "xxxx", authorities
				.toArray(new GrantedAuthority[authorities.size()]));

		SecurityContextHolder.getContext().setAuthentication(token);
	}

	/**
	 * {@inheritDoc}
	 */
	public void destroy() {
	}
}
